package com.penjin.android.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.penjin.android.R;
import com.penjin.android.activity.gongzuo.PenjinApplyActivity;
import com.penjin.android.activity.kaoqin.GerenKaoqinActivity;
import com.penjin.android.activity.kaoqin.GerenPaibanActivityNew;
import com.penjin.android.activity.kaoqin.KaoqinTongjiActivity;
import com.penjin.android.domain.PenjinCompany;
import com.penjin.android.domain.PenjinDate;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.service.UserService;
import com.penjin.android.utils.BitmapUtils;
import com.penjin.android.view.CircleImageView;

import java.util.Date;

public class KaoqinFragmentNew extends Fragment implements View.OnClickListener {


    View gerenKaoqin;
    View yueKaoqin;
    View kaoqinShengqing;
    View kaoqinTongji;

    RelativeLayout banner;
    TextView companyName;
    CircleImageView avatar;
    TextView day;
    TextView date;

    PenjinCompany company = null;
    PenjinUser user = null;
    PenjinDate penjinDate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_fragment_kaoqin_new1, container, false);
        initView(rootView);
        initData();
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    private void initData() {
        UserService userService = UserService.getInstance(getActivity().getApplicationContext());
        user = userService.getCurrentUser();
        if (user != null) {
            if (user.isInCompany()) {
                company = userService.getCurrentCompany();
            }
        }
        penjinDate = new PenjinDate(new Date());
        if (user != null) {
            BitmapUtils.loadUrlImage(user.getTouxiang(), avatar, getActivity(), false);
        }
        if (company != null) {
            companyName.setText(company.getCompanyName());
        }
        int result = penjinDate.day;
        switch (result) {
            case 1:
                day.setText("星期一");
                break;
            case 2:
                day.setText("星期二");
                break;
            case 3:
                day.setText("星期三");
                break;
            case 4:
                day.setText("星期四");
                break;
            case 5:
                day.setText("星期五");
                break;
            case 6:
                day.setText("星期六");
                break;
            case 0:
                day.setText("星期日");
                break;
        }
        date.setText(penjinDate.year + "年" + penjinDate.month + "月" + penjinDate.date + "日");
    }

    private void initView(View rootView) {
        banner = (RelativeLayout) rootView.findViewById(R.id.banner);
        banner.setOnClickListener(this);
        banner.setBackgroundDrawable(new BitmapDrawable(BitmapUtils.readBitMap(getActivity(), R.drawable.pj_kaoqin_banner2)));
        companyName = (TextView) rootView.findViewById(R.id.companyName);
        avatar = (CircleImageView) rootView.findViewById(R.id.avatar);
        day = (TextView) rootView.findViewById(R.id.day);
        date = (TextView) rootView.findViewById(R.id.date);

        gerenKaoqin = rootView.findViewById(R.id.gerenKaoqin);
        yueKaoqin = rootView.findViewById(R.id.yueKaoqin);
        kaoqinShengqing = rootView.findViewById(R.id.kaoqinShengqing);
        kaoqinTongji = rootView.findViewById(R.id.kaoqingtongji);


        gerenKaoqin.setOnClickListener(this);
        yueKaoqin.setOnClickListener(this);
        kaoqinShengqing.setOnClickListener(this);
        kaoqinTongji.setOnClickListener(this);

        ((ImageView)rootView.findViewById(R.id.line1)).setImageBitmap(BitmapUtils.readBitMap(getActivity(),R.drawable.pj_kaoqin_apply));
        ((ImageView)rootView.findViewById(R.id.line2)).setImageBitmap(BitmapUtils.readBitMap(getActivity(),R.drawable.pj_kaoqin_user));
        ((ImageView)rootView.findViewById(R.id.line3)).setImageBitmap(BitmapUtils.readBitMap(getActivity(),R.drawable.pj_kaoqin_calendar));
        ((ImageView)rootView.findViewById(R.id.line4)).setImageBitmap(BitmapUtils.readBitMap(getActivity(),R.drawable.pj_kaoqin_tongji));
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.gerenKaoqin:
                intent = new Intent(getActivity(), GerenKaoqinActivity.class);
                startActivity(intent);
                break;
            case R.id.yueKaoqin:
                intent = new Intent(getActivity(), GerenPaibanActivityNew.class);
                startActivity(intent);
                break;
            case R.id.kaoqinShengqing:
                intent = new Intent(getActivity(), PenjinApplyActivity.class);
                startActivity(intent);
                break;
            case R.id.kaoqingtongji:
                intent = new Intent(getActivity(), KaoqinTongjiActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
